import { createSlice, PayloadAction } from '@reduxjs/toolkit';
import { fetchRegistration } from './auth.actions';


const initialState = {
	user: {
		token: '',
		data: { email: '', password: '', fullName: '', id: '' }
	},
	loading: false,
	error: null,
	success: false
};

export const userAuthorizationSlice = createSlice({
	name: 'userRegistration',
	initialState,
	reducers: {},
	extraReducers: (builder) => {
		builder.addCase(fetchRegistration.pending, (state) => {
			state.loading = true;
			state.error = null;
		}),
		builder.addCase(
			fetchRegistration.fulfilled,
			(state, action) => {
				state.user = action.payload;
				state.success = true;
			}
		),
		builder.addCase(fetchRegistration.rejected, (state, action) => {
			// console.log('ошибка из слайса регистрации', action.payload);
			state.error = action.error.message;
		});
	}
});

export default userRegistrationSlice.reducer;
